package org.zfes.snowier.cloud.zbss.layer.tc.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.idcenter.IDGenerator;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.IProgressService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.dao.ProgressMapper;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.Progress;

@Service
public class ProgressServiceImpl  implements IProgressService{
 	@Autowired
	private ProgressMapper progressMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createProgress(Progress progress) {
		ZBeanUtil.validateBean(progress);
		progress.setId(IDGenerator.genLongId());
		progressMapper.insertSelective(progress);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Progress> loadProgressById(Long id) {
		if(id!=null){
			return Optional.ofNullable(progressMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateProgress(Progress progress) {
		ZBeanUtil.validateBean(progress);
		progressMapper.updateByPrimaryKeySelective(progress);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletProgress(Long id) {
		if(id!=null){
			progressMapper.deleteByPrimaryKey(id);
			return;
		}
		ZAlert.Error("请选择要删除的数据");
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadProgressDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS(progressMapper.selectListCount(pm), progressMapper.selectList(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	@Override
	public void setEnabled(List<Long> ids, Boolean enabled) {
		ZAssert.notNull(ids, "请选择数据");
		ZAssert.notNull(enabled, "状态参数不能为空");
		int value=enabled?1:0;
		progressMapper.updateEnabled( ids,  value);
		
	}
	
}